/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Point;
import java.util.ArrayList;

public class Piece {
	
	/**
	 * Class constants for specifying the direction of the piece.
	 */
	public static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
	
	/**
	 * The type of piece this piece is.
	 */
	private String type;
	
	/**
	 * How this piece should be treated by the rules.
	 */
	private String ruleType;
	
	/**
	 * How this piece should be represented in the user interface.
	 */
	private String representation;
	
	/**
	 * The initial state of the piece.
	 */
	private State initialState;
	
	/**
	 * All the pieces this piece can be upgraded to.
	 */
	private ArrayList<String> upgrades = new ArrayList<String>();
	
	/**
	 * All the moves this piece can make.
	 */
	private ArrayList<Move> moves = new ArrayList<Move>();
	
	/**
	 * Get the type of piece this piece is.
	 * @return The type of piece this piece is.
	 */
	public String getType() {
		return this.type;
	}
	
	public String getRuleType() {
		if (this.ruleType == null) {
			return "";
		}
		else {
			return this.ruleType;
		}
	}
	
	public String getRepresentation() {
//		TODO Make everything else work with this:
		return this.representation;
	}
	
	public State getInitialState() {
		return this.initialState;
	}
	
	/**
	 * Get all the available upgrades for this piece.
	 * @return An ArrayList of the Pieces this piece can be upgraded to.
	 */
	public ArrayList<String> getUpgradeOptions() {
//		TODO Make everything else work with this:
		return new ArrayList<String>(this.upgrades);
	}
	
	/**
	 * Check whether or not the piece can perform the suggested upgraded.
	 * @param type The upgrade you wish to perform.
	 * @return True if the upgrade can be performed, false if it can't.
	 */
	public boolean hasUpgrade(String type) {
		for (String upgradeOption : this.getUpgradeOptions()) {
			if (upgradeOption.equals(type)) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Get the piece's currently available movement options.
	 * @return The piece's own movement options, or the upgrade's movement option if it has been upgraded.
	 */
	public ArrayList<Move> getMoves() {
		if (this.initialState.getUpgrade() == this) {
			return this.moves;
		}
		else {
			return this.initialState.getUpgrade().getMoves();
		}
	}
	
//	/**
//	 * Add a move to the piece.
//	 * @param move The move you wish to add.
//	 */
//	protected void addMove(Move move) {
//		this.moves.add(move);
//	}
	
	/**
	 * Get the move that can bring the piece to the specified destination.
	 * @param distance The x, y distance to the destination.
	 * @return The Move that can bring the piece to the specified destination. Null if no such move exists.
	 */
	public Move getMove(Point distance) {
		for (Move move : this.getMoves()) {
			int repetitions = Piece.getRequiredRepetitions(move, distance);
			if (repetitions > 0) {
				return move;
			}
		}
		
		return null;
	}
	
	/**
	 * Return the number of repetitions required to reach the destination with the specified move.
	 * @param move The move which you wish to use to reach the destination.
	 * @param distance The x, y distance to the destination.
	 * @return 0 if it's already there, -1 if it can't get there, or the number of repetitions required in order to get there.
	 */
	public static int getRequiredRepetitions(Move move, Point relativeDistance) {
		
//		TODO This method returns incorrect values. Fix it.
		
		int repetitionsX = Piece.computeRepetitionsX(relativeDistance.x, move.getDistance().x);
		int repetitionsY = Piece.computeRepetitionsY(relativeDistance.y, move.getDistance().y);
		
		if (repetitionsX == -1 || repetitionsY == -1) {
			return -1;
		}
		else if (repetitionsX == 0 && repetitionsY == 0) {
			return 0;
		}
		else if (repetitionsX == repetitionsY) {
			return repetitionsX;
		}
		else if (repetitionsX == 0 && repetitionsY > 0) {
			return repetitionsY;
		}
		else if (repetitionsX > 0 && repetitionsY == 0) {
			return repetitionsX;
		}
		else {
			return -1;
		}
	}
	
	/**
	 * Compute the number of repetitions required to cover the the specified distance at the specified speed.
	 * @param distanceX The distance.
	 * @param speedX The speed.
	 * @return 0 if it's already there, a negative number if it can't get there, or the number of repetitions required in order to get there.
	 */
	public static int computeRepetitionsX(int distanceX, int speedX) {
		if (Math.abs(speedX) > Math.abs(distanceX)) {
			return -1;
		}
		else if (speedX != 0 && distanceX % speedX == 0) {
			return distanceX / speedX;
		}
		else if (speedX == 0 && distanceX == 0) {
			return 0;
		}
		else {
			return -1;
		}
	}
	
	/**
	 * Compute the number of repetitions required to cover the the specified distance at the specified speed.
	 * @param distanceY The distance.
	 * @param speedY The speed.
	 * @return 0 if it's already there, a negative number if it can't get there, or the number of repetitions required in order to get there.
	 */
	public static int computeRepetitionsY(int distanceY, int speedY) {
		if (Math.abs(speedY) > Math.abs(distanceY)) {
			return -1;
		}
		else if (speedY != 0 && distanceY % speedY == 0) {
			return distanceY / speedY;
		}
		else if (speedY == 0 && distanceY == 0) {
			return 0;
		}
		else {
			return -1;
		}
	}
	
	public Piece(String name, String rule_type, String representation, ArrayList<Move> moves, ArrayList<String> upgrades, State initialState) {
		this.type = name;
		this.ruleType = rule_type;
		this.representation = representation;
		this.moves = moves;
		this.upgrades = upgrades;
		this.initialState = initialState;
	}
	
	public Piece(Piece piece) {
		this(piece.type, piece.ruleType, piece.representation, new ArrayList<Move>(piece.moves), new ArrayList<String>(piece.upgrades), null);
	}
	
	public Piece(Piece piece, State initialState) {
		this(piece.type, piece.ruleType, piece.representation, new ArrayList<Move>(piece.moves), new ArrayList<String>(piece.upgrades), initialState);
	}
	
	public String toString() {
		return this.getType();
	}

	public boolean hasRuleType() {
		return this.ruleType != null;
	}

	void setInitialState(State initialState) {
		this.initialState = initialState;
	}
}
